package com.hspedu.com.jdbc;

import com.mysql.jdbc.Driver;

import java.io.FileWriter;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

/**
 * @author DL5O
 * @version 1.0
 * 这是第一个Jdbc程序，完成简单的操作
 */
public class Jdbc01 {
    public static void main(String[] args) throws Exception{

        //1.注册驱动
        Driver driver = new Driver();//创建一个driver对象

        //2.得到连接
        //String url = "jdbc:mysql://localhost:3306/ywl_db02";
        String url = "jdbc:mysql://localhost:3306/ywl_db02";
        Properties properties = new Properties();
        properties.setProperty("user","root");//用户名
        properties.setProperty("password","123456");//密码
        properties.store(new FileWriter("src\\jdbc.properties"),null);
        Connection connect = driver.connect(url, properties);

        //3.执行sql
        //String sql = "insert into actor values(null,'刘德华','男','1970-11-11','110001616')";
        String sql = "update actor set name='周星驰',sex='男',borndate='1990-02-03' where id = '2'";

        //statement 用于执行静态的sql语句并返回sql对象
        Statement statement = connect.createStatement();
        int rows = statement.executeUpdate(sql);//如果是 dml语句，返回的就是影响的行数
        if(rows != 0){
            System.out.println("操作成功..");
        }else {
            System.out.println("操作失败..");
        }


        //关闭连接资源
        statement.close();
        connect.close();
    }
}
